//路由文件
//导入express
const express = require("express");
//调用router方法
const router = express.Router();
//导入处理用户登录逻辑的js文件
const userlogin = require("../service/userlogin")
const authorsList = require("../service/authorsList");
const articleList = require("../service/articleList");
const deleteArticle = require("../service/deleteArticle");
const classifyArticleList = require("../service/classifyArticleList")
const addUser = require("../service/addUser");
const addArticle = require("../service/addArticle");
// //设置路由 登录接口
router.post("/userlogin",(req,res)=>{
    //获取用户传递过来的参数
    let params = req.body
    //console.log(params)
    let username = params.userName //用户名
    let userpwd = params.userpassword //密码
    //调用处理登录逻辑的函数，将用户名和密码传递过去
    userlogin(username,userpwd).then(data=>{
        if(data==501){
            res.json({
                status:data,
                msg:"用户不存在"
            })
        }else if(data==502){
            res.json({
                status:data,
                msg:"密码错误"
            })
        }else{
            res.json({
                status:200,
                msg:{_id:Buffer.from(String(element.userId)).toString("base64"),
                     userName:data.userName,
                     headImg:data.headImg}
            })
        }
    })
})
//获取首页所有作者列表的路由
router.get("/authorsList",(req,res)=>{
    authorsList().then(data=>{
        if(data==4004){
            res.json({
                status:data,
                msg:"作者列表为空"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
            res.end()
        }

    })
    //res.send("ok")
})
//获取首页所有文章内容的路由
router.get("/articleList",(req,res)=>{
    articleList().then(data=>{
        if(data==3003){
            res.json({
                status:data,
                msg:"作者列表为空"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
            res.end()
        }

    })
    //res.send("ok")
})
// //删除文章接口
router.get("/deleteArticle",(req,res)=>{
    let params = req.body;
    let authorId = params.authorId;
    deleteArticle(authorId).then(data=>{
        if(data==402){
            res.json({
                status:data,
                msg:"请删除"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:"true"
    })
    res.end()
}
})
})
//文章分类页接口
router.get("/classifyArticleList",(req,res)=>{
    let params = req.body;
    let classify = params.classify;
    articleList(classify).then(data=>{
        if(data==2002){
            res.json({
                status:data,
                msg:"列表为空"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
            res.end()
        }

    })
    //res.send("ok")
})
//处理用户注册接口
router.post("/addUser",(req,res)=>{
    //获取用户传递过来的参数
    let params = req.body
    //console.log(params)
    let username = params.userName //用户名
    let userId = params.userId
    let headImg = params.headImg
    let createTime = params.createTime
    let desc = params.addUser_desc
    addUser(username,userId,headImg,createTime,desc).then(data=>{
        if(data==505){
            res.json({
                status:data,
                msg:"注册失败"
            })
        }else if(data==506){
            res.json({
                status:data,
                msg:"注册成功"
            })
        }
    })
})
//新增文件接口
router.post("/addArticle",(req,res)=>{
    let params = req.body;
    let article_id = params._id
    let title = params.title
    let desc = params.article_desc
    let author = params.author
    let authorId = params.authorId
    let classify = params.classify
    let content = params.content
    addArticle(article_id,title,desc,author,authorId,classify,content).then(data=>{
        if(data==405){
            res.json({
                status:data,
                msg:"数据库错误"
            })
            res.end()
        }else{
            res.json({
                status:406,
                msg:"添加成功"
    })
    res.end()
}
})
})
//导出
module.exports=router;

